package m.jclecai.com.fragment.follow;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.base.ViewHolerAdapter;
import m.jclecai.com.adapter.holder.ViewHolder;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.Response.ResConcernList;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by Dinosa on 2017/11/20.
 * <p>
 * 我的关注
 */

public class MineConcernFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2{

    @BindView(R.id.pr_listview)
    PullToRefreshListView mPrListview;

    private int mPosition;
    private MyConcernAdapter mConcernListAdapter;
    private int mPage=1;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine_concern;
    }


    public void setPosition(int position) {
        mPosition = position;
    }


    @Override
    public void initData() {
        super.initData();
        requestData();
    }

    /**
     * 向服务器请求数据；
     */
    private void requestData() {

        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user", PreferenceUtil.getUserName(getActivity()));
        paramsMap.put("password", PreferenceUtil.getPassword(getActivity()));
        HttpManager.postAsyn(Constant_Url.MY_CONCERN, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络连接异常!!!");
                mPrListview.onRefreshComplete();
            }

            @Override
            public void onResponse(String response) {
                if (!TextUtils.isEmpty(response)) {
                    //这里我们对数据进行要给解析
                    ResConcernList resHitList = FastOk.get(response, ResConcernList.class);
                    if (resHitList != null && "0".equals(resHitList.getStatus())) {

                        List<ResConcernList.ConcernBean> hitList = resHitList.getRecords();
                        int totalPages=Integer.parseInt(resHitList.getPages());

                        mConcernListAdapter.add(hitList);

                        if(mPage<totalPages){
                            mPage+=1;
                        }
                    } else {
                        DisPlay(resHitList.getMessage());
                    }

                } else {
                    DisPlay("网络连接异常！！！");
                }
                mPrListview.onRefreshComplete();
            }
        }, paramsMap);
    }

    @Override
    public void initView() {
        super.initView();
        mConcernListAdapter = new MyConcernAdapter();
        mPrListview.setAdapter(mConcernListAdapter);
        mPrListview.setOnRefreshListener(this);
        mPrListview.setMode(PullToRefreshBase.Mode.BOTH);
    }

    @Override
    public void initListener() {
        super.initListener();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

        mConcernListAdapter.clear();

        requestData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        requestData();
    }

    public class MyConcernAdapter extends ViewHolerAdapter<ConcernViewHolder> {

        public List<ResConcernList.ConcernBean> mData = new ArrayList<>();


        public void refreshData(List<ResConcernList.ConcernBean> data) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }

        public void clear(){
            mData.clear();
            notifyDataSetChanged();
        }


        public void add(List<ResConcernList.ConcernBean> data) {
            mData.addAll(data);
            notifyDataSetChanged();
        }

        @Override
        public ConcernViewHolder onCreateViewHolder(int position, ViewGroup parent) {
            return new ConcernViewHolder(getActivity(), R.layout.item_list_concern);
        }

        @Override
        public void onBindViewHolder(int position, ConcernViewHolder concernViewHolder) {
            concernViewHolder.init(mData.get(position));
        }

        @Override
        public int getCount() {
            return mData.size();
        }

    }

    public class ConcernViewHolder extends ViewHolder {

        private final Context mContext;
        @BindView(R.id.iv_user_icon)
        ImageView mIvUserIcon;

        @BindView(R.id.tv_username)
        TextView mTvUsername;

        @BindView(R.id.tv_bet_history)
        TextView mTvBetHistory;

        @BindView(R.id.tv_fans_count)
        TextView mTvFansCount;

        @BindView(R.id.tv_unit_award)
        TextView mTvUnitAward;

        @BindView(R.id.tv_is_follow)
        TextView mTvIsFollow;

        @BindView(R.id.tv_follow_number)
        TextView mTvFollowNumber;

        public ConcernViewHolder(Context context, @LayoutRes int resource) {
            super(context, resource);
            mContext = context;
        }

        public void init(ResConcernList.ConcernBean bean) {
            mTvUsername.setText(bean.getUsername());
            String military = bean.getMilitary();
            String[] split = military.split("/");
            mTvBetHistory.setText("近7天战绩:" + split[0] + "中" + split[1]);

            Glide.with(MineConcernFragment.this).load(bean.getUser_face()).into(mIvUserIcon);
            //这里我们判断是否可以跟单；
            if ("0".equals(bean.getUser_project_follow())) {
                mTvFollowNumber.setVisibility(View.INVISIBLE);
                mTvIsFollow.setEnabled(false);
            } else {
                mTvIsFollow.setEnabled(true);
                mTvFollowNumber.setText(bean.getUser_project_follow());
                mTvFollowNumber.setVisibility(View.VISIBLE);
            }

            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //这里是跳转到具体的详情页面中去；
                }
            });
        }
    }


}
